package cz.zcu.fav.rat.managed.bean.calendar;

import java.util.Date;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.model.ScheduleModel;

import cz.zcu.fav.rat.beans.User;
import cz.zcu.fav.rat.managed.bean.calendar.event.UserSpecificReservationFactory;
import cz.zcu.fav.rat.security.beans.UserSessionController;

@ManagedBean(name = "objectUserCalendarBean")
@ViewScoped
public class ObjectUserCalendarBean extends ObjectCalendarBean
{
	private static final long serialVersionUID = -864494482091498463L;
	private User currentUser;

	@Override
	protected ScheduleModel getModel(Integer identifier)
	{
		return new CalendarModelEventObject(identifier, new UserSpecificReservationFactory(getCurrentUser()));
	}
	
	public User getCurrentUser()
	{
		if (currentUser == null)
		{
			Integer currentUserId = UserSessionController.getLoggedUserBean()
					.getId();
			for (User user : getUsers())
			{
				if (currentUserId.equals(user.getId()))
				{
					currentUser = user;
					break;
				}
			}
		}
		return currentUser;
	}

	@Override
	protected ReservationEvent createNewEvent(Date since, Date to)
	{
		return new ReservationEvent("dummy", since, to, getCurrentUser(),
				getItem(), null, null);
	}
}
